#include "Print.h"
#include <stdio.h>
#include <stdint.h>

struct FormatOut_t FormatOut;


static void printhex(char * mseeage, uint8_t *hexdat, uint8_t length, char * end)
{
	printf(">>%s", mseeage);
	for (int i = 0; i < length; i++)
	{
		if (hexdat[i] <= 0x0F)
		{
			printf("0");
		}
		printf("%x", hexdat[i]);
		//if (i % 2 == 1)
		//{
			printf(" ");
		//}
	}
	if (end != NULL)
	{
		printf("%s", end);
	}
}
static void print2hex(char * mseeage, uint16_t *hexdat, uint8_t length, char * end)
{
	printf(">>%s", mseeage);
	for (int i = 0; i < length; i++)
	{
		if (hexdat[i] <= 0x000F)
		{
			printf("000");
		}
		else if (hexdat[i] <= 0x00ff)
		{
			printf("00");
		}
		else if (hexdat[i] <= 0x0fff)
		{
			printf("0");
		}
		printf("%x", hexdat[i]);
		printf(" ");
	}
	if (end != NULL)
	{
		printf("%s", end);
	}
}
static void printdec(char * mseeage, uint32_t decdata, char * end)
{
	printf(">>%s", mseeage);
	printf(" %d ", decdata);
	if (end != NULL)
	{
		printf("%s", end);
	}
}
static void printok(void)
{
	printf(">> OK\r\n");
}
static void printerror(uint8_t msg)
{
	printf(">> Error(%d)\r\n", msg);
}
static void printInvalid()
{
	printf(">> Invalid\r\n");
}
static void printbusy(uint8_t msg)
{
	printf(">> Busy(%d)\r\n", msg);
}
static void printmessage(char * msg)
{
	printf(">> %s\r\n", msg);
}
static void loginfo(char * msg,uint32_t data,uint16_t rank)
{
	if (data == NULL)
	{
		data = 666;
	}
	switch (rank)
	{
	case 1 :
	{
		printf(">>Message:>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>[%s](%d)\r\n", msg,data);
		break;
	}
	case 2:
	{
		printf(">>Message:>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>[%s](%d)\r\n", msg, data);
		break;
	}
	case 3:
	{
		printf(">>Message:>>>>>>>>>>>>>>>>>>>>>>>>>[%s](%d)\r\n", msg, data);
		break;
	}
	case 4:
	{
		printf(">>Message:>>>>>>>>>>>>[%s](%d)\r\n", msg, data);
		break;
	}
	default :
	{
		printf(">>Message:[%s](%d)\r\n", msg, data);
		break;
	}
	}
}

void FormatOutInit(void)
{
	FormatOut.OutOK = printok;
	FormatOut.OutBusy = printbusy;
	FormatOut.OutError = printerror;
	FormatOut.OutHex = printhex;
	FormatOut.OutDec = printdec;
	FormatOut.LogInfo = loginfo;
	FormatOut.OuttMessage = printmessage;
	FormatOut.Print = printf;
	FormatOut.Out2Hex = print2hex;
	FormatOut.OutInvalid = printInvalid;
	//FormatOut.OutOK();
}


